import lodash from 'lodash';
import * as data from './data';
import log from '../../../log';
import * as util from '../../../util';

const likeParms = 'content'.split(',');
export function fetch(query, page, pageSize) {
    return util.resolve().then(() => {
        log.info('fetch appsettings params----', query);
        const pageOpt = {
            page,
            pageSize
        };
        let where = [];
        if (lodash.keys(query).length > 0) {
            lodash.mapKeys(query, (value, key) => {
                if (likeParms.indexOf(key) >= 0) {
                    where.push([`${key}`, 'like', `%${value}%`]);
                } else {
                    where.push([`${key}`, '=', `${value}`]);
                }
            });
        }
        if (where.length < 1) {
            where = null;
        }
        return data.fetch(where, 1, pageOpt).then(res => {
            log.info('fetch appsettings result----', res.toJSON());
            return util.data(res, 1);
        });
    }).catch(err => {
        log.error('fetch appsettings is err----', err);
        return err;
    });
}
export function find(query) {
    return util.resolve().then(() => {
        log.info('find appsettings params----', query);
        let where = [];
        if (lodash.keys(query).length > 0) {
            lodash.mapKeys(query, (value, key) => {
                if (likeParms.indexOf(key) >= 0) {
                    where.push([`${key}`, 'like', `%${value}%`]);
                } else {
                    where.push([`${key}`, '=', `${value}`]);
                }
            });
        }
        if (where.length < 1) {
            where = null;
        }
        return data.find(where, 1).then(res => {
            log.info('find appsettings result----', util.data(res));
            return util.data(res);
        });
    }).catch(err => {
        log.error('find appsettings is err----', err);
        return err;
    });
}
export function add(appsettings, ctx) {
    return util.resolve().then(() => {
        log.info('add appsettings params----', appsettings);
        appsettings.id = util.uuid();
        return data.add(appsettings).then(res => {
            log.info('add appsettings result----', util.data(res));
            return util.data(res);
        });
    }).catch(err => {
        log.error('add appsettings is err----', err);
        return ctx.throw(500, err);
    });
}
export function edit(appsettings, ctx) {
    return util.resolve().then(() => {
        log.info('edit appsettings params----', appsettings);
        return data.edit(appsettings).then(res => {
            log.info('edit appsettings result----', util.data(res));
            return util.data(res);
        });
    }).catch(err => {
        log.error('edit appsettings is err----', err);
        return ctx.throw(500, err);
    });
}
export function del(id) {
    return util.resolve().then(() => {
        log.info('delete appsettings params----', id);
        return data.del(id).then(res => {
            log.info('delete appsettings result----success', util.data(res));
            return util.data(res);
        });
    }).catch(err => {
        log.error('delete appsettings is err----', err);
        return err;
    });
}
